package model

import (
	"goim/server/system/global"
	"goim/server/util"
)

type FriendModel struct {
	Base
}

type Friend struct {
	Fid         uint64 `gorm:"primaryKey;column:fid;type:int(12)"`
	Frienda     uint64 `gorm:"column:frienda;type:int(12);not null;"`
	Friendb     uint64 `gorm:"column:friendb;type:int(12);not null"`
	Create_time uint64 `gorm:"column:create_time;type:int(12);not null"`
	Status      uint8  `gorm:"column:status;type:int(1);not null"`
}

type FriendListData struct {
	Fid             uint64
	Friend_id       uint64
	Friend_sex      uint8
	Friend_age      uint8
	Friend_username string
	Friend_nickname string
	Friend_realname string
	Friend_avatar   string
}

func (m *FriendModel) tabName() string {
	return global.JLZS_CONFIG.Mysql.Prefix + "friend"
}

// 好友资料详情
func (m *FriendModel) Detail(id uint64, where map[string]string) (User, error, bool) {

	var user User
	err := global.JLZS_DB.Where(&User{Phone: where["phone"]}).First(&user)
	if err.Error != nil {
		return user, err.Error, false
	}
	return user, err.Error, true
}

// 好友列表
func (m *FriendModel) ListData(id uint64, where map[string]string) ([]FriendListData, error, bool) {
	var tabN string = m.tabName()
	var friend []Friend
	err := global.JLZS_DB.Raw("SELECT * FROM `" + tabN + "` WHERE frienda=" + where["fid"] + " OR Friendb=" + where["fid"]).Scan(&friend)
	if err.Error != nil {
		return nil, err.Error, false
	} else {
		var friLen int = len(friend)
		var reFriend []FriendListData = make([]FriendListData, 0)
		var tmpFriend FriendListData
		userM := new(UserModel)
		var reUser User

		for i := 0; i < friLen; i++ {
			if id == friend[i].Frienda {
				tmpFriend.Friend_id = friend[i].Friendb
			} else if id == friend[i].Friendb {
				tmpFriend.Friend_id = friend[i].Frienda
			}

			if tmpFriend.Friend_id > 0 {
				reUser, _, _ = userM.Detail(tmpFriend.Friend_id, nil)
				tmpFriend.Fid = friend[i].Fid
				tmpFriend.Friend_username = reUser.Username
				tmpFriend.Friend_nickname = reUser.Nickname
				tmpFriend.Friend_realname = reUser.Realname
				tmpFriend.Friend_sex = reUser.Sex
				tmpFriend.Friend_age = reUser.Age
				tmpFriend.Friend_avatar = reUser.Avatar
			}
			reFriend = append(reFriend, tmpFriend)
		}
		return reFriend, nil, true
	}
}

// 陌生人列表(添加好友)
func (m *FriendModel) ListStranger(id uint64, where map[string]string, fid string) []User {
	var tabN string = m.tabName()
	var friend []Friend
	err := global.JLZS_DB.Raw("SELECT * FROM `" + tabN + "` WHERE frienda=" + where["fid"] + " OR Friendb=" + where["fid"]).Scan(&friend)
	if err.Error != nil {
		return nil
	} else {
		var friLen int = len(friend)
		friendId := make([]uint64, 0)
		for i := 0; i < friLen; i++ {
			if id == friend[i].Frienda {

				friendId = append(friendId, friend[i].Friendb)
			} else if id == friend[i].Friendb {
				friendId = append(friendId, friend[i].Friendb)
			}
		}
		userM := new(UserModel)
		friendId = append(friendId, id)
		return userM.StrangerList(friendId, fid)
	}
}

// 新增用户
func (m *FriendModel) StrangerAdd(data map[string]string) bool {
	var tName string = m.tabName()
	var sqla string = "SELECT * FROM `" + tName + "` WHERE frienda=" + data["fid"] + "AND friendb=" + data["stranger_id"]
	var sqlb string = "SELECT * FROM `" + tName + "` WHERE frienda=" + data["stranger_id"] + "AND friendb=" + data["fid"]

	var friend []Friend
	global.JLZS_DB.Raw(sqla).Scan(&friend)
	if len(friend) > 0 {
		return false
	}

	global.JLZS_DB.Raw(sqlb).Scan(&friend)
	if len(friend) > 0 {
		return false
	}

	createFriendDb := &Friend{
		Frienda:     util.StrToUint64(data["fid"]),
		Friendb:     util.StrToUint64(data["stranger_id"]),
		Create_time: uint64(util.Time()),
		Status:      0,
	}
	result := global.JLZS_DB.Create(&createFriendDb)
	if result.Error != nil {
		return false
	}
	return true
}

// 删除
func (m *FriendModel) Del(fid uint64) bool {

	tabName := m.tabName()
	db := global.JLZS_DB.Exec("DELETE FROM `"+tabName+"` WHERE fid= ? ", fid)
	if db.Error != nil {
		return false
	} else {
		return true
	}
}
